using System;
using System.Text;
using System.Windows.Forms;

using Tino.Controls;
using Tino.Core;

namespace Tino
{
    public partial class EntryEditorForm : Form
    {
        ImageList images;
        int imageIndex = -1;
        int entryID;

        #region Public properties members

        public string Title
        {
            get { return titleTextBox.Text; }
        }

        public string Content
        {
            get { return contentTextBox.RtfText; }
        }

        public int ImageIndex
        {
            get { return imageIndex; }
        }

        public int[] TagsIDs
        {
            get { return (int[])tagsTextBox.Tag; }
        }

        #endregion

        public EntryEditorForm(ImageList Images) : this(-1, Images) { }

        public EntryEditorForm(int EntryID, ImageList Images)
        {
            InitializeComponent();

            entryID = EntryID;
            images = Images;
            
            // Set window title.
            Text = (entryID == -1 ? "Add" : "Edit") + " entry";

            if (entryID > -1)
                load();
        }

        public void PreFillTagsSelection(TagsListItemsCollection tags)
        {
            tagsTextBox.Text = tags.ToString();
            tagsTextBox.Tag = tags.ToArray();
        }

        void load()
        {
            LoadEntryCommand command = new LoadEntryCommand(entryID, titleTextBox, contentTextBox, tagsTextBox);
            command.Execute();

            Entry e = new Entry();
            imageIndex = e.GetEntryImageIndex(entryID);
            if (imageIndex > -1)
            {
                pickIconButton.Image = images.Images[imageIndex];
            }
        }

        bool validate()
        {
            if (titleTextBox.Text == "")
            {
                MessageBox.Show("Give a title", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        void pickIconButton_Click(object sender, EventArgs e)
        {
            IconPickerForm iconPicker = new IconPickerForm(images);
            if (iconPicker.ShowDialog() == DialogResult.OK)
            {
                imageIndex = iconPicker.SelectedImageIndex;
                if (imageIndex > -1)
                {
                    pickIconButton.Image = images.Images[imageIndex];
                }
            }
            iconPicker.Dispose();
        }

        void chooseButton_Click(object sender, EventArgs e)
        {
            TagsSelectionForm selectTags = new TagsSelectionForm((int[])tagsTextBox.Tag, tagsTextBox.Text);
            if (selectTags.ShowDialog() == DialogResult.OK)
            {
                tagsTextBox.Text = selectTags.SelectedTags.ToString();
                tagsTextBox.Tag = selectTags.SelectedTags.ToArray();
            }
            selectTags.Dispose();
        }

        void okButton_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        void contentTextBox_FormattingError(object sender, FormattingErrorEventArgs e)
        {
            // A formatting error occured, show it to the client.
            MessageBox.Show(e.Exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        void Form_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        } 
    }
}